package com.链表.练习题;

public class Leetcode82 {
    /**
     * 删除链表中重复的元素II
     *
     * 给定一个已排序的链表的头 head ，
     * 删除原始链表中所有重复数字的节点，只留下不同的数字 。返回 已排序的链表 。
     *
     * 输入：head = [1,2,3,3,4,4,5]
     * 输出：[1,2,5]
     */
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode deleteDuplicates(ListNode head){
        if(head == null || head.next == null) return head;

        ListNode s = new ListNode(-1,head);
        ListNode p1 = s;
        ListNode p2;
        ListNode p3;
        while ((p2 = p1.next) != null && (p3 = p2.next) != null) {
            if (p2.val == p3.val) {
                while ((p3 = p3.next) != null && (p3.val == p2.val)) {

                }
                p1.next = p3;
            }else {
                p1 = p1.next;
            }
        }
        return  s.next;
    }

}
